package com.ruoyi.base.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import com.ruoyi.base.domain.BaseTeacher;
import com.ruoyi.base.service.IBaseTeacherService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 老师Controller
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Api("老师信息管理")
@RestController
@RequestMapping("/base/teacher")
public class BaseTeacherController extends BaseController
{
    @Autowired
    private IBaseTeacherService baseTeacherService;

    /**
     * 查询老师列表
     */
    @ApiOperation("获取老师列表")
    @PreAuthorize("@ss.hasPermi('base:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseTeacher baseTeacher)
    {
        startPage();
        List<BaseTeacher> list = baseTeacherService.selectBaseTeacherList(baseTeacher);
        return getDataTable(list);
    }
    
    /**
     * 查询老师下拉列表
     */
    @ApiOperation("获取老师下拉列表")
    @GetMapping("/select")
    public TableDataInfo select(BaseTeacher baseTeacher)
    {
        List<BaseTeacher> list = baseTeacherService.selectBaseTeacherList(baseTeacher);
        return getDataTable(list);
    }

    /**
     * 导出老师列表
     */
    @ApiOperation("导出老师列表")
    @PreAuthorize("@ss.hasPermi('base:teacher:export')")
    @Log(title = "老师", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseTeacher baseTeacher)
    {
        List<BaseTeacher> list = baseTeacherService.selectBaseTeacherList(baseTeacher);
        ExcelUtil<BaseTeacher> util = new ExcelUtil<BaseTeacher>(BaseTeacher.class);
        return util.exportExcel(list, "教师");
    }

    /**
     * 获取老师详细信息
     */
    @ApiOperation("获取老师详细信息")
    @ApiImplicitParam(name = "id", value = "老师ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:teacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseTeacherService.selectBaseTeacherById(id));
    }
    
    /**
     * 获取老师详细信息
     */
    @ApiOperation("获取老师详细信息")
    @ApiImplicitParam(name = "id", value = "老师用户ID", required = true, dataType = "int", paramType = "path")
    @GetMapping(value = "/user/{userId}")
    public AjaxResult getByUser(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(baseTeacherService.selectBaseTeacherById(userId));
    }

    /**
     * 新增老师
     */
    @ApiOperation("新增老师")
    @PreAuthorize("@ss.hasPermi('base:teacher:add')")
    @Log(title = "老师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BaseTeacher baseTeacher)
    {
        return toAjax(baseTeacherService.insertBaseTeacher(baseTeacher));
    }

    /**
     * 修改老师
     */
    @ApiOperation("修改老师")
    @PreAuthorize("@ss.hasPermi('base:teacher:edit')")
    @Log(title = "老师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BaseTeacher baseTeacher)
    {
        return toAjax(baseTeacherService.updateBaseTeacher(baseTeacher));
    }

    /**
     * 删除老师
     */
    @ApiOperation("删除老师")
    @ApiImplicitParam(name = "id", value = "老师ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:teacher:remove')")
    @Log(title = "老师", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseTeacherService.deleteBaseTeacherByIds(ids));
    }
    
    
    /**
     * 禁用教师
     */
    @ApiOperation("禁用教师")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:teacher:edit')")
    @Log(title = "教师", businessType = BusinessType.UPDATE)
	@GetMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids)
    {
        return toAjax(baseTeacherService.updateBaseTeacherStatus(ids, "0"));
    }
    
    /**
     * 启用课程
     */
    @ApiOperation("启用教师")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:teacher:edit')")
    @Log(title = "教师", businessType = BusinessType.UPDATE)
	@GetMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids)
    {
        return toAjax(baseTeacherService.updateBaseTeacherStatus(ids, "1"));
    }
    
    @Log(title = "导入教师信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:teacher:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseTeacher> util = new ExcelUtil<BaseTeacher>(BaseTeacher.class);
        List<BaseTeacher> teacherList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = baseTeacherService.importTeacher(teacherList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<BaseTeacher> util = new ExcelUtil<BaseTeacher>(BaseTeacher.class);
        return util.importTemplateExcel("教师数据");
    }
}
